package com.bstek.bdf4.enums;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;

/**
 * @Title: IBaseEnum
 * @Package: com.demo
 * @Description:
 * @Author: zxq
 * @Date: 2023/10/30 - 16:54
 */

public interface IBaseEnum<T> {

    default  void initEnum(T code, String msg){
        EContainer.addEnum(this,code,msg);
    }

    default T getCode(){
        return   EContainer.getEnum(this).getCode();
    }

    default String getMsg(){
        return  EContainer.getEnum(this).getMsg();
    }


    static<T,R extends IBaseEnum<T>> R getByCode(Class<? extends IBaseEnum<T>> clazz, T code){
           return  Stream.of(clazz.getEnumConstants())
                   .filter(a->a.getCode().equals(code))
                   .map(v->(R)v)
                   .findAny()
                   .orElse(null);
    }

    class EContainer{
        private static final Map<IBaseEnum, EnumBean> maps=
                new ConcurrentHashMap<>();


        static<T> void addEnum(IBaseEnum<T> iBaseEnum,
                                       T code,String msg){
            maps.put(iBaseEnum,new EnumBean(code,msg));
        }

        static <K extends IBaseEnum<T>,T> EnumBean<T> getEnum(K k){
            return  maps.get(k);
        }


    }

}


